//
//  ScrollerComplexController.m
//  YXLearnMasonry
//
//  Created by Rookie_YX on 16/10/27.
//  Copyright © 2016年 Rookie_YX. All rights reserved.
//

#import "ScrollerComplexController.h"

@interface ScrollerComplexController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *expandStates;
@property (nonatomic, strong) UILabel *lableL;

@end

@implementation ScrollerComplexController

- (void)viewDidLoad {
    [super viewDidLoad];
  
  self.scrollView = [[UIScrollView alloc] init];
  self.scrollView.pagingEnabled = NO;
   self.scrollView.backgroundColor = [UIColor whiteColor];
  [self.view addSubview:self.scrollView];
 
  for (NSUInteger i = 0; i < 10; ++i) {
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.layer.borderColor = [UIColor greenColor].CGColor;
    label.layer.borderWidth = 2.0;
    label.text = [self randomLabletext];
    label.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
    [label addGestureRecognizer:tap];
    
    label.preferredMaxLayoutWidth = kScreenWidth - 30;
    label.textAlignment = 0;
    label.textColor = RANDOMCOLOR;
    [self.scrollView addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.equalTo(10);
      make.right.equalTo(self.view).offset(-10);
      
      if (self.lableL) {
        make.top.equalTo(self.lableL.mas_bottom).offset(10);
      } else {
        make.top.equalTo(self.scrollView).offset(10);
      }
      
      make.height.equalTo(40);
    }];
    
    self.lableL = label;
   //  加入到数组中,并把状态设置为 NO
    [self.expandStates addObject:@[label, @(NO)].mutableCopy];
  }
  
  [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.view);
    // 让scrollview的contentSize随着内容的增多而变化
    make.bottom.equalTo(self.lableL.mas_bottom).offset(20);
  }];

}

- (NSString *)randomLabletext{
  CGFloat textLength = arc4random() % 60 + 10;
  NSMutableString *str = [[NSMutableString alloc] init];
  for (NSUInteger i = 0; i < textLength; ++i) {
    [str appendFormat:@"点我,点我"];
  }
  return str;
}

- (void)onTap:(UITapGestureRecognizer *)sender {
  UIView *tapView = sender.view;
  
  NSUInteger index = 0;
  //  遍历
  for (NSMutableArray *array in self.expandStates) {
    // 数组第一个存的是 Lable控件
    UILabel *view = [array firstObject];
    
    if (view == tapView) {
      // 数组最一个存的是 Lable控件状态
      NSNumber *state = [array lastObject];
      
      // 当前是展开状态的话，就收缩
      if ([state boolValue] == YES) {
        [view mas_updateConstraints:^(MASConstraintMaker *make) {
          make.height.mas_equalTo(40);
        }];
      } else {
        UIView *preView = nil; // 当前
        UIView *nextView = nil; // 下一个
    
        //  点击的是当前的,展开
        if (index - 1 < self.expandStates.count && index >= 1) {
          preView = [[self.expandStates objectAtIndex:index - 1] firstObject];
        }
        // 点击是下一个, 展开
        if (index + 1 < self.expandStates.count) {
          nextView = [[self.expandStates objectAtIndex:index + 1] firstObject];
        }
        
        [view mas_remakeConstraints:^(MASConstraintMaker *make) {
          if (preView) {  // 当前
            make.top.equalTo(preView.mas_bottom).offset(10);
          } else {
            make.top.equalTo(10);
          }
          
          make.left.equalTo(10);
          make.right.equalTo(self.view).offset(-10);
        }];
        
        if (nextView) {  // 下一个
          [nextView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view.mas_bottom).offset(10);
          }];
        }
      }
      //  取反赋值, 二次点击时候,进行缩放
      [array replaceObjectAtIndex:1 withObject:@(!state.boolValue)];
      
      [self.view setNeedsUpdateConstraints];
      [self.view updateConstraintsIfNeeded];
      
      [UIView animateWithDuration:0.35 animations:^{
        [self.view layoutIfNeeded];
      } completion:^(BOOL finished) {
        
      }];
      break;
    }
    index++;
  }
}

#pragma mark - getting
- (NSMutableArray *)expandStates {
if (_expandStates == nil) {
  _expandStates = [[NSMutableArray alloc] init];
}

return _expandStates;
}

@end
